<?php

    /**
     * Class Navigate_model
     */
    class Tool
    {
        function __construct()
        {

        }
        static function makeString($length=6)
        {
            $str="3456789abcdfghijkmnprstuvwxyABDFGHJKMNPQRTUY";
            return substr(str_shuffle($str),0,$length);
        }
        static function returnOk( $result = '' )
        {
            return [ 'isok' => true, 'error' => '', 'result' => $result ];
        }

        static function returnError( $error = '', $error_code = '', $result = '' )
        {
            return [ 'isok' => false, 'error' => $error, 'error_code' => $error_code, 'result' => $result ];
        }

        static function exitOk( $rows = '',$total='' )
        {
            self::log(1);
            exit(json_encode([ 'isok' => true, 'rows' => $rows , 'total'=>$total],JSON_UNESCAPED_UNICODE));
        }

        static function exitError( $error = '', $error_code = '',$location='', $result = '' )
        {
            self::log(0,$error);
            exit( json_encode( [ 'isok' => false, 'error' => $error, 'error_code' => $error_code, 'location'=>$location,'result' => $result ],JSON_UNESCAPED_UNICODE ) );
        }
        static function log($isok='',$error='')
        {
            $ci= &get_instance();//ci控制器超级对象，就是把所有执行的方法打印出来
            $arr['option_directory'] = substr($ci->router->directory,0,-1); //分组目录
            if(strtolower($arr['option_directory'])!='admin')
                return false;
            $arr['admin_id'] = isset($_SESSION[ 'admin_id' ])?$_SESSION[ 'admin_id' ]:'';
            $arr['admin_name'] = isset($_SESSION[ 'admin_name' ])?$_SESSION[ 'admin_name' ]:'';
            $arr['option_controller'] = $ci->router->class;   //当前控制器
            $arr['option_method'] = $ci->router->method;    // 当前使用方法
            $arr['option_url'] = $_SERVER['PHP_SELF'];
            $arr['option_get']=json_encode($_GET,JSON_UNESCAPED_UNICODE);
            $arr['option_post']=json_encode($_POST,JSON_UNESCAPED_UNICODE);
            $arr['option_time']=date("Y-m-d H:i:s");
            $arr['option_ip']=$ci->input->ip_address();
            $arr['status']=$isok;
            $arr['desc']=$error;
            $ci->db->insert( 'admin_log',$arr );
        }

        static function p($str,$style=0)
        {
            if(!$style)
            {
                echo "<pre>";
            }
            print_r($str);
            exit();
        }
        static function v($str,$style=0)
        {
            if(!$style)
            {
                echo "<pre>";
            }
            print_r($str);
        }
    }

?>